package plugin.redis;

import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;

/**
 * Created by zqx on 16/3/3.
 */
public class RedisDataUtils {

    public static String select(int db) {
        String result = null;

        Jedis jedis = RedisDataSource.getJedis();
        if (jedis == null) {
            return result;
        }
        try {
            result = jedis.select(db);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisDataSource.release(jedis);
        }
        return result;
    }

    /**
     * 设置单个值
     *
     * @param key
     * @param value
     * @return
     */
    public static String set(String key, String value) {
        String result = null;

        Jedis jedis = RedisDataSource.getJedis();
        if (jedis == null) {
            return result;
        }
        try {
            result = jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisDataSource.release(jedis);
        }
        return result;
    }

    /**
     * 获取单个值
     *
     * @param key
     * @return
     */
    public static String get(String key) {
        String result = null;
        Jedis jedis = RedisDataSource.getJedis();
        if (jedis == null) {
            return result;
        }

        try {
            result = jedis.get(key);

        } catch (Exception e){
            e.printStackTrace();
        } finally {
            RedisDataSource.release(jedis);
        }
        return result;
    }

    public static Boolean exists(String key) {
        Boolean result = false;
        Jedis jedis = RedisDataSource.getJedis();
        if (jedis == null) {
            return result;
        }
        try {
            result = jedis.exists(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisDataSource.release(jedis);
        }
        return result;
    }

    public static Long del(String key) {
        Long result = -1L;
        Jedis jedis = RedisDataSource.getJedis();
        if (jedis == null) {
            return result;
        }
        try {
            result = jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisDataSource.release(jedis);
        }
        return result;
    }

    public static String hmset(String key, Map<String, String> hash) {
        String result = null;
        Jedis jedis = RedisDataSource.getJedis();
        if (jedis == null) {
            return result;
        }
        try {
            result = jedis.hmset(key, hash);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisDataSource.release(jedis);
        }
        return result;
    }

    public static List<String> hmget(String key, String... fields) {
        List<String> result = null;
        Jedis jedis = RedisDataSource.getJedis();
        if (jedis == null) {
            return result;
        }
        try {
            result = jedis.hmget(key, fields);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisDataSource.release(jedis);
        }
        return result;
    }

    public static Long hdel(String key, String contentkey) {
        Long result = null;
        Jedis jedis = RedisDataSource.getJedis();
        if (jedis == null) {
            return result;
        }
        try {

            result = jedis.hdel(key, contentkey);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisDataSource.release(jedis);
        }
        return result;
    }

    public static Long hincrBy(String key, String field, int value) {
        Long result = null;
        Jedis jedis = RedisDataSource.getJedis();
        if (jedis == null) {
            return result;
        }
        try {
            jedis.hincrBy(key, field, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisDataSource.release(jedis);
        }
        return result;
    }

    public static Long hlen(String key) {
        Long result = null;
        Jedis jedis = RedisDataSource.getJedis();
        if (jedis == null) {
            return result;
        }
        try {

            result = jedis.hlen(key);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisDataSource.release(jedis);
        }
        return result;
    }

    /**
     * 设置到期时间？
     * @param key
     * @param seconds
     * @return
     */
    public static Long expire(String key, int seconds) {
        Long result = null;
        Jedis jedis = RedisDataSource.getJedis();
        if (jedis == null) {
            return result;
        }
        try {
            result = jedis.expire(key, seconds);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisDataSource.release(jedis);
        }
        return result;
    }

    public static Long expireAt(String key, long unixTime) {
        Long result = null;
        Jedis jedis = RedisDataSource.getJedis();
        if (jedis == null) {
            return result;
        }
        try {
            result = jedis.expireAt(key, unixTime);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RedisDataSource.release(jedis);
        }
        return result;
    }


}
